<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){ 
        var_dump($_SESSION);
    	$question = D('vercode')->getOne();
    	$this->assign('question',$question);
        $this->assign('title',"登录");
        $this->display();
    }
    //异步检测数据，进行登录
    public function checkdata()
    {

    	$this->checkVercode();

    	$email = I('post.email');

        // var_dump(I('post.pass'));

    	$pass = md5(I('post.pass'));

    	$sql = "select * from user where email='{$email}' and password = '{$pass}'";
    	// var_dump($sql);

    	$res = M()->query($sql);

    	if($res){
    		$loginTime = time();

    		$sql = "update user set login_time='{$loginTime}' where id='{$res[0][id]}'";

    		M()->execute($sql);

    		$_SESSION['nikename']=$res[0]['nikename'];
    		$_SESSION['id']=$res[0]['id'];
    		$_SESSION['face']=$res[0]['face'];
            if(isset($_COOKICE['return_url'])){
                $url = $_COOKICE['return_url'];
                unset($_COOKICE['return_url']);
            }else{
                $url = U('home/set/index');
            }
            $arr = array(
             'error'=>'0',
             'info'=>'登录成功',
             'url'=>$url,
             );
        }else{
            $arr = array(
             'error'=>'1',
             'info'=>'登录失败'
             );
        }

    	echo json_encode($arr);
    }
    //退出
    public function loginout()
    {
    	session_unset();
    	session_destroy();
    	$this->success('退出成功', U('home/index/index'),4);
    }

    public function qqcall()
    {
        $openid = getOpenid();

        $sql = "select * from user where openid='{$openid}'";

        $res = M()->query($sql);

        if($res){
            $_SESSION['nikename']=$res[0]['nikename'];
            $_SESSION['id']=$res[0]['id'];
            $_SESSION['face']=$res[0]['face'];
            $this->success('登录成功',U('home/set/index'));
        }else{
            $info = getUserInfo();

            $info['sex'] = $info['gender'];

            $info['create_time'] =time();

            $info['create_ip'] = get_client_ip();

            $info['face'] = getQQFace($info['figureurl_2']);

            $info['nikename'] = $info['nickname'];

            $info['openid'] = $openid;

            $res = M('user')->add($info);

            if($res){
                $_SESSION['nikename']=$info['nickname'];
                $_SESSION['id']=$res;
                $_SESSION['face']=$info['face'];
                $this->success('新帐号登录成功',U('home/set/index'));
            }else{
                $this->success('登录失败',U('home/login/index'));
            }
        } 
    }

    public function qqlogin()
    {
        if(C('IS_QQLOGIN')){
            qq_login();
        }else{
            $this->error("QQ登录关闭");
        }
        
        
    }
}